package com.hurl.waguji.web.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.hurl.library.util.DateUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.biz.schedule.ProcessJobDetail;
import com.hurl.waguji.biz.schedule.ScheduleService;
import com.hurl.waguji.web.extbo.GridData;
import com.opensymphony.xwork2.ActionSupport;

public class ScheduleAction extends ActionSupport{
	private GridData gridData = new GridData();
	private String jobId;
	private String result;
	private String executorClass;
	private String cronExpression;
	private Map<String,Object> jobClassNames = new HashMap<String,Object>();

	private static Map<String,ProcessJobDetail> processJobDetailMap = ProcessJobLoader.load();
	
	public Map<String, Object> getJobClassNames() {
		return jobClassNames;
	}

	public void setJobClassNames(Map<String, Object> jobClassNames) {
		this.jobClassNames = jobClassNames;
	}

	public String getExecutorClass() {
		return executorClass;
	}

	public void setExecutorClass(String executorClass) {
		this.executorClass = executorClass;
	}

	public String getCronExpression() {
		return cronExpression;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public GridData getGridData() {
		return gridData;
	}

	public void setGridData(GridData gridData) {
		this.gridData = gridData;
	}
	
	public String schedule(){
		try{
			ProcessJobDetail detail = processJobDetailMap.get(getJobId()).clone();
			detail.setCronExpression(getCronExpression());
			ScheduleService.scheduleProcessJob(detail);
			result = "1";
			return SUCCESS;
		}catch(Exception e){
			result = e.getMessage();
			return SUCCESS;
		}
		
	}
	public String unschedule()throws Exception{
		if(StringUtils.isBlank(jobId)){
			result = "请先选择要启动的任务！";
		}else{
			if(!ScheduleService.unscheduleJob(jobId)){
				result = "删除失败！";
			}else{
				result = "1";
			}
		}
		return SUCCESS;
	}
	public String start()throws Exception{
		if(StringUtils.isBlank(jobId)){
			result = "请先选择要启动的任务！";
		}else{
			if(!ScheduleService.triggerJob(jobId)){
				result = "启动失败！";
			}else{
				result = "1";
			}
		}
		return SUCCESS;
	}
	
	public String query() throws Exception {
		List<ProcessJobDetail> jobList = ScheduleService.getAllJobs();
		gridData.setTotalProperty(jobList.size());
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(ProcessJobDetail item : jobList){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("jobId",item.getIdentifier().getId());
			map.put("jobTitle", item.getIdentifier().getTitle());
			map.put("cronExpression", item.getCronExpression());
			map.put("nextFireTime", item.getNextFireTime()!=null?DateUtils.formatDate(item.getNextFireTime(),DateUtils.FORMAT_DATE_TIME_SIMPLE):"");
			map.put("previousFireTime", item.getPreviousFireTime()!=null?DateUtils.formatDate(item.getPreviousFireTime(),DateUtils.FORMAT_DATE_TIME_SIMPLE):"");
			map.put("status", item.getStatus());
			list.add(map);
		}
		gridData.setRoot(list);
		return SUCCESS;
	}
	
	public String jobClassNames()throws Exception{
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		Iterator<Entry<String,ProcessJobDetail>> it = processJobDetailMap.entrySet().iterator();
		while(it.hasNext()){
			Entry<String,ProcessJobDetail> entry = it.next();
			Map<String,String> item = new HashMap<String,String>();
			item.put("value", entry.getKey());
			item.put("label", entry.getValue().getIdentifier().getTitle());
			list.add(item);
		}
		jobClassNames.put("totalProperty", list.size());
		jobClassNames.put("root", list);
		return SUCCESS;
	}
}
